#!/usr/bin/env sh
#============HEADER=================================================|
#AUTHOR
#  Jefferson Rocha <lrcjefferson@gmail.com>
#
#PROGRAM
#  CRYPORANGE - Acid Cryptography
#
#DESCRIPTION
#
#
#TEST-ON
#  bash, zsh, ksh [OK]
#
#CHANGELOG
#  V2.0 (Jefferson Rocha)
#    - add variable RANDOM 'uhul', for more randomness.
#    - add 2 functions, shuffles and deshuffles for to change
#    some letters.
#    - base32 is also being used, with base64.
#===================================================================|

#=====VARS===========================|
version="2.0"
archive="/tmp/.archive${RANDOM}"
uhul="$RANDOM"
#=Colors
bold=$(tput bold)
back_red=$(tput setab 1)
back_green=$(tput setab 2)
back_blue=$(tput setab 4)
back_cyan=$(tput setab 6)
fore_red=$(tput setaf 1)
fore_green=$(tput setaf 2)
end_color=$(tput sgr0)
#====================================|


#=====TESTS==========================|
for conference in "base64" "base32" "tr" "rev"; do # Exist list? no bye!
   if ! type -p "$conference" >/dev/null 2>&1; then
      printf '%b' "${bold}${fore_red}NEED '${conference}' TO CONTINUE${end_color}\n"
      exit 1
   fi
done
#====================================|


#=====FUNCTIONS======================|
remove(){
  if [ -e "$archive" ];then
      rm "$archive" >/dev/null 2>&1
  fi
}

_USAGE(){
cat <<END | less
CRYPORANGE  $version
User Manual.

What is cryporange?
   Cryporange uses simple, more functional encryption!
   As a base it has the cipher of cesar, reverse and base64.
   United all on one base, that's cryptorange! good fun!


ENCRYPT
   To encrypt a message, you must select option (1), after you write
   your message, for end, press <ENTER> then CTRL + D for encrypt your
   message.


DECRYPT
   To decrypt a message, you must select option (2) menu.
   After this you paste your message encrypted, press <ENTER> then CTRL + D
   for decrypt.


AUTHOR
   2018 Jefferson Rocha <lrcjefferson@gmail.com>

BUGS
   Send a message to lrcjefferson at gmail dot com

(RETURN? PRESS Q)

END
}

shuffles(){
   sed \
     "s/ /++!@/g; s/,/$uhul/g; s/a/t-!/g; s/e/$-/g; s/i/__/g; s/r/I@/g"
}

deshuffles(){
   sed \
     "s/++!@/ /g; s/$uhul/,/g; s/t-!/a/g; s/$-/e/g; s/__/i/g; s/I@/r/g"
}

_crypt(){
    printf '%b' "+-+-+-+- ENCRYPT MODE -+-+-+-+\n"
    printf '%b\n' "${bold}${fore_red}Enter TEXT. (NEW LINE and CTRL+D for CRYPT MESSAGE).${end_color}"
    # Print stdout.
    cat > "$archive"              # Write a text
    [ ! -e "$archive" ] && exit 0 # Archive no exit? bye.
    cat "$archive"         | \
    tr '[a-z]' '[x-za-w]'  | \
    shuffles               | \
    base32                 | \
    rev                    | \
    base64
    remove  # Remove .archive in /tmp
}

_decrypt(){
    printf '%b' "+-+-+-+- DECRYPT MODE -+-+-+-+\n"
    printf '%b\n' "${bold}${fore_red}PASTE TEXT: (NEW LINE and CTRL+D for DECRYPT MESSAGE).${end_color}"
    cat > "$archive"
    [ ! -e "$archive" ] && exit 0  # Archive no exist? BYE.
    cat "$archive"           | \
    base64 -d                | \
    rev                      | \
    base32 -d                | \
    deshuffles               | \
    tr '[x-za-w]' '[a-z]'
    remove   # Remove .archive in /tmp
}
#====================================|

# MAIN
remove  # Remove .archive in /tmp
clear   # <3 Clean
cat << RANGER
${bold}${fore_green}
____ ____ _   _ ___  ____ ____ ____ _  _ ____ ____
|    |__/  \_/  |__] |  | |__/ |__| |\ | | __ |__
|___ |  \   |   |    |__| |  \ |  | | \| |__] |___

Acid Cryptography V${version} ${back_red}  ${back_green}  ${back_blue}  ${back_cyan}  ${end_color}
____________________________________________________

RANGER

PS3=$'\n> '
select menu in "Encrypt" "Decrypt" "Usage" "Exit"; do
    if [ "$REPLY" = "1" ]; then
        _crypt   # Call Func
    elif [ "$REPLY" = "2" ]; then
        _decrypt # Call Func
    elif [ "$REPLY" = "3" ]; then
        _USAGE   # Call Func
    elif [ "$REPLY" = "4" ]; then
        exit 0   # Bye
    else
       printf '%b' "\t${bold}${fore_red}INVALID OPTION!!!!${end_color}\n"
    fi
done
